package com.wx.mythread.stopthread;

/**
 * 最佳操作2：
 * 在catch捕获InterruptedException异常后，应该重新调用Thread.currentThread()
 * .isInterrupted()来
 */
public class RightWayStopThreadWithInProd2 implements Runnable {
    @Override
    public void run() {
        while (true){
            System.out.println("程序还在执行");
            if (Thread.currentThread().isInterrupted()){
                System.out.println("interrupted");
                break;
            }
            reInterrupted();
        }
    }

    private void reInterrupted() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithInProd2());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }
}
